package com.xpfirst;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;

import com.xpfirst.R;
import com.xpfirst.arm.ARMBiZhang;
import com.xpfirst.arm.ARMJNI;
import com.xpfirst.arm.ARMＴemp;
import com.xpfirst.socket.SocketManage;
import com.xpfirst.socket.SocketServer;
import com.xpfirst.ActivityManage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ARMServerActivity extends Activity {
    public Button socket; 
    public Button fromHost; 
    public Button sendMsg;
    public Button writeMsg;
    public TextView readText;
    public TextView writeText;
    public ARMJNI armjni;
    public ARMＴemp armtemp;
    public ARMBiZhang armbizhang;
    private static final String TAG = "TakeActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	ActivityManage.h = h;
        init();
        listener();
    }    
    public void init(){
        socket   = (Button)this.findViewById(R.id.socket);
        fromHost   = (Button)this.findViewById(R.id.fromHost);
        sendMsg   = (Button)this.findViewById(R.id.sendMsg);
        writeMsg   = (Button)this.findViewById(R.id.writeMsg);
        readText   = (TextView)this.findViewById(R.id.readText);
        writeText   = (TextView)this.findViewById(R.id.writeText);
        armjni = new ARMJNI();
        
        armtemp = new ARMＴemp();
		Thread tempThread=new Thread(armtemp);
		tempThread.start();
		
        armbizhang = new ARMBiZhang();
		Thread bizhangThread=new Thread(armbizhang);
		bizhangThread.start();
		
    	int fd = ARMJNI.openElec();
    	Log.v("open elec", String.valueOf(fd));
    }
    private void listener(){
    	socket.setOnClickListener(openSocket);
    };
    private Button.OnClickListener openSocket = new Button.OnClickListener(){
		public void onClick(View v){
	        socket.setText(SocketManage.getLocalIpAddress());
	        SocketManage.listen();
		}
    };
	public Handler h=new Handler(){
        public void handleMessage(Message msg) {
        	String str;
            switch (msg.what) {
                case 0:
                	str = msg.getData().getString("temp");
                	str = "temp:"+str;
                	writeText.setText(str);
                    SocketManage.write(str);
                    break;
                case 1:
                	str = msg.getData().getString("bizhang");
                	str = "bizhang:"+str;
                	readText.setText(str);
                    SocketManage.write(str);
                    break;
                case 2:
                	str = msg.getData().getString("host");
                	fromHost.setText(str);
                    break;
                case 3:
                	str = msg.getData().getString("sendMsg");
                	sendMsg.setText(str);
                    break;
                case 4:
                	str = msg.getData().getString("writeMsg");
                	writeMsg.setText(str);
                    for(int i = 0;i <13;i++){
                    	ARMJNI.ioctlElec(i, 0);
                    }
                    String[] strs = str.split("-");
                    String str0 = strs[0];
                    String str1 = strs[1];
                    int int0,int1;
                    if(!str0.equals(" ")){
                    	int0 = Integer.parseInt(str0);
                    	ARMJNI.ioctlElec(int0, 1);
                    }
                    if(!str1.equals(" ")){
                    	int1 = Integer.parseInt(str1);
                    	ARMJNI.ioctlElec(int1, 1);
                    }
                    break;
            }
            super.handleMessage(msg);
        }
	}; 
}